package cn.zcib.film.controller;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.Notice;
import cn.zcib.film.bean.vo.NoticeVO;
import cn.zcib.film.service.NoticeService;
import cn.zcib.film.util.DataGridViewResult;
import cn.zcib.film.util.SystemConstants;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    /**
     * 公告列表
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult getList(NoticeVO vo){
        //设置分页信息
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        //调用获取公告列表方法
        List<Notice> notices = noticeService.getList(vo);
        //创建分页对象
        PageInfo<Notice> pageInfo = new PageInfo<>(notices);

//        System.out.println("pageInfo = " + pageInfo);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("/publish")
    public String publish(Notice notice){
        HashMap<String, Object> map = new HashMap<>();
        if (noticeService.insertNotice(notice)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "发布成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "发布失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/update")
    public String update(Notice notice){
        HashMap<String, Object> map = new HashMap<>();
        if (noticeService.updateNotice(notice)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "修改成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/del")
    public String del(Notice notice){
        HashMap<String, Object> map = new HashMap<>();
        if (noticeService.delNotice(notice)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "删除成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/listLimit")
    public List<Notice> listLimit(){
        return noticeService.listLimit();
    }

}
